// pages/service/index.js

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlService: app.urlService,
    contentH: 0,
    modalShow: false,
    date: undefined,
    time: undefined,
    orderData: [],
    consignee: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    var that = this;
    that.setData({
      contentH: winHeight
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderList();
  },


  getOrderList: function () {
    var that = this;
    wx.showLoading();
    wx.request({
      url: that.data.urlService + '/api/v1/order/getOrderList?pageNum=1&pageSize=1000',
      method: "GET",
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (data) {
        if (data.statusCode == 401) {
          wx.showToast({
            title: '登录超时，请重新登录',
            icon: "error"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
          return;
        }
        wx.hideLoading()
        var json = JSON.parse(data.data);
        console.log(json.data.list);
        if (json.success) {
          that.setData({
            orderData: json.data.list
          })
        } else {
          wx.showToast({
            title: json.msg,
            icon: 'error'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  addOrder: function () {
    wx.navigateTo({
      url: "../addOrder/index"
    })
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  sureGood: function (e) {
    this.setData({
      id: e.currentTarget.dataset.id,
      modalShow: true
    })
  },

  closeModal: function () {
    this.setData({
      modalShow: false
    })
  },

  inputConsignee: function (e) {
    this.setData({
      consignee: e.detail.value
    })
  },

  returnOrder: function () {
    var that = this;
    var param = {
      id: that.data.id,
      consignee: that.data.consignee,
      consigneeDt: that.data.date + " " + that.data.time
    }
    wx.showLoading();
    wx.request({
      url: that.data.urlService + '/api/v1/order/returnOrder',
      method: "POST",
      data: param,
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (data) {
        if (data.statusCode == 401) {
          wx.showToast({
            title: '登录超时，请重新登录',
            icon: "error"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
          return;
        }
        wx.hideLoading()
        var json = JSON.parse(data.data);
        console.log(json.data);
        if (json.success) {
          that.setData({
            modalShow: false
          })
          that.getOrderList();
        } else {
          wx.showToast({
            title: json.msg,
            icon: 'error'
          })
        }
      }
    })
  }
})